package com.tutu.service.impl;

import com.tutu.core.Constant;
import com.tutu.core.api.CompanyBalanceApiUrls;
import com.tutu.core.api.DeviceSummaryApiUrls;
import com.tutu.service.CompanyBalanceService;
import com.tutu.utils.HttpRequestUtil;
import com.tutu.utils.UnicodeUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Service
public class CompanyBalanceServiceImpl implements CompanyBalanceService {

    @Override
    public Map<String, Object> pageByParams(Integer companyId, String keyWord, Integer pageNum, Integer pageSize) {
        return HttpRequestUtil.getOnlyData(CompanyBalanceApiUrls.base_prefix + CompanyBalanceApiUrls.pageWithBalance, "companyId=" + companyId + "&keyWord=" + UnicodeUtil.unicodeEncode(keyWord) + "&pageNum=" + pageNum + "&pageSize=" + pageSize, "valueList");
    }

    @Override
    public Map<String, Object> pageDataByParams(Integer companyBalanceId, Integer pageNum, Integer pageSize) {
        return HttpRequestUtil.getOnlyData(CompanyBalanceApiUrls.base_prefix + CompanyBalanceApiUrls.pageWithStatic, "companyBalanceId=" + companyBalanceId + "&pageNum=" + pageNum + "&pageSize=" + pageSize, "valueList");
    }

    @Override
    public Map<String, Object> saveInfo(HttpServletRequest request, Integer id, Integer companyId, Integer codeDictType, String codeDictName, double count) {
        LogInfoServiceImpl.saveLogInfo(request, "编辑公司排放量，ID：" + id + "，公司ID：" + companyId + "，因子类型：" + codeDictType + "，因子名称：" + codeDictName + "，排放量：" + count);

        return HttpRequestUtil.getCompleteData(CompanyBalanceApiUrls.base_prefix + CompanyBalanceApiUrls.updateCompanyBalance, "id=" + id + "&companyId=" + companyId + "&codeDictType=" + codeDictType + "&codeDictName=" + UnicodeUtil.unicodeEncode(codeDictName) + "&count=" + count);
    }

    @Override
    public Map<String, Object> batchAdd(HttpServletRequest request, Integer deviceId, String emissionCodes, String values) {
        LogInfoServiceImpl.saveLogInfo(request, "批量添加公司排放量，设备ID：" + deviceId + "，因子编码：" + emissionCodes + "，排放量：" + values);

        return HttpRequestUtil.getCompleteData(CompanyBalanceApiUrls.base_prefix + CompanyBalanceApiUrls.batchUpdateCompanyBalance, "deviceId=" + deviceId + "&emissionCodes=" + emissionCodes + "&values=" + values);
    }

    @Override
    public Map<String, Object> deleteById(HttpServletRequest request, Integer id) {
        LogInfoServiceImpl.saveLogInfo(request, "删除公司排放量，ID：" + id);

        return HttpRequestUtil.getCompleteData(CompanyBalanceApiUrls.base_prefix + CompanyBalanceApiUrls.delete + "CompanyBalance", "id=" + id);
    }

    @Override
    public Map<String, Object> batchDel(HttpServletRequest request, String ids) {
        LogInfoServiceImpl.saveLogInfo(request, "批量删除公司排放量，IDS：" + ids);

        return HttpRequestUtil.getCompleteData(CompanyBalanceApiUrls.base_prefix + CompanyBalanceApiUrls.batchDel, "ids=" + ids);
    }

    @Override
    public Map<String, Object> companyBalanceStatic(HttpServletRequest request) {
        return HttpRequestUtil.getCompleteData(DeviceSummaryApiUrls.base_prefix + DeviceSummaryApiUrls.deviceDataStatic, "companyId=" + request.getSession().getAttribute(Constant.CURRENT_LOGIN_COMPANY_ID));
    }

    @Override
    public Map<String, Object> findBalanceListByCompanyId(Integer companyId) {
        return HttpRequestUtil.getCompleteData(CompanyBalanceApiUrls.base_prefix + CompanyBalanceApiUrls.findBalanceListByCompanyId, "companyId=" + companyId, "company", "valueList");
    }

    @Override
    public Map<String, Object> saveChildInfo(HttpServletRequest request, Integer id, Integer status) {
        LogInfoServiceImpl.saveLogInfo(request, "更新排放量统计子项的显示or隐藏状态，ID：" + id + "，状态：" + status);

        return HttpRequestUtil.getCompleteData(CompanyBalanceApiUrls.base_prefix + CompanyBalanceApiUrls.updateStaticStatus, "id=" + id + "&status=" + (null == status || status.equals(Constant.CHECK_STATUS_DEFAULT) ? Constant.CHECK_STATUS_SUCCESS : Constant.CHECK_STATUS_DEFAULT));
    }
}
